package cn.itcast.service;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

//@Service("userDetailsService")
@Component("myUserDetailsService")
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder pw;


    @Reference
    private UserService userService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        cn.itcast.pojo.User user = userService.Login(username);

        if (user==null){
            throw new UsernameNotFoundException("用户名或密码错误");
        }
       String password = pw.encode(user.getPassword());
       return new User(username,password,
               AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));
    }
}

